How to create Customer attribute programmatically in magento 2?

Let’s we Create a simple Mobile number Customer attribute in magento 2. We need to create simple module for add new customer attribute in magento 2.

Create registration.php file to register our module. File Path, app/code/Rbj/CustomerAttribute/registration.php


Create module.xml file defined our module setup version to setup_module table. Create module.xml file defined our module setup version to setup_module table. File Path, app/code/Rbj/CustomerAttribute/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  <module name="Rbj_CustomerAttribute" setup_version="1.0.0">
      <module name="Magento_Customer"/>

We need to define our mobile customer attribute into extension_attributes.xml file. File path,app/code/Rbj/CustomerAttribute/etc/extension_attributes.xml 

Pass Classname inside for attribute in below xml as Magento\Customer\Api\Data\CustomerInterface because Its base Customer interface for create customer attribute in Magento 2. Set attribute code as your customer attribute code and define type as string.

<?xml version="1.0" ?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="mobile" type="string"/>


For Create Customer attibute we need to define InstallData.php file to add entry in database of our attribute.
File Path, app/code/Rbj/CustomerAttribute/Setup/InstallData.php


namespace Rbj\CustomerAttribute\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class InstallData implements InstallDataInterface

    private $customerSetupFactory;

     * Constructor
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;

     * {@inheritdoc}
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'mobile', [
            'type' => 'varchar', // type of attribute
            'label' => 'Mobile Number',
            'input' => 'text', // input type
            'source' => '',
            'required' => false, // if you want to required need to set true
            'visible' => true,
            'position' => 500, // position of attribute
            'system' => false,
            'backend' => ''
        /* Specify which place you want to display customer attribute */
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'mobile')
        ->addData(['used_in_forms' => [

Now Run the upgrade command to install our module.
You need to run command using SSh from the root of your Magento instance,

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:flush

Now you can check your new mobile number attribute at admin panel.
Login with admin credential,
Go To, Customers -> All Customers -> Edit any Customer.