One place for hosting & domains

      Local

      Easy Node Authentication: Setup and Local

      Note: This article is part of our Easy Node Authentication series.

      Introduction

      Authentication and logins in Node can be a complicated thing. Actually logging in for any application can be a pain. This article series will deal with authenticating in your Node application using the package Passport.

      Note: Updates

      Edit 11/18/2017: Updated to reflect Facebook API changes. Updating dependencies in package.json.
      Edit #1: Changed password hashing to be handled inside user model and asynchronously.
      Edit #2: Changed password hashing to be explicitly called. Helps with future tutorials.

      What we’ll be building:

      We will build an application that will have:

      • Local account logins and signups (using passport-local)
      • Facebook logins and registration (using passport-facebook)
      • Twitter logins and registration (using passport-twitter)
      • Google+ logins and registration (using oauth with passport-google-oauth)
      • Require login for certain routes/sections of your application
      • Creating a password hash for local accounts (using bcrypt-nodejs)
      • Displaying error messages (using flash with connect-flash. required since express 3.x)
      • Linking all social accounts under one user account
      • Allowing a user to unlink a specific social account

      Enough chit-chat. Let’s dive right into a completely blank Node application and build our entire application from scratch.

      Here’s what we’ll be building:

      And after a user has logged in with all their credentials:

      For this article, we’ll be focusing on setup and only local logins and registrations/signups. Since this is the first article and also deals with setting up our application, it will probably be one of the longer ones if not the longest. Sit tight for the duration of your flight.

      Setting Up Our Application

      To set up our base Node application, we’ll need a few things. We’ll set up our npm packages, node application, configuration files, models, and routes.

              - app
              ------ models
              ---------- user.js  <!-- our user model -->
              ------ routes.js    <!-- all the routes for our application -->
              - config
              ------ auth.js      <!-- will hold all our client secret keys (facebook, twitter, google) -->
              ------ database.js  <!-- will hold our database connection settings -->
              ------ passport.js  <!-- configuring the strategies for passport -->
              - views
              ------ index.ejs    <!-- show our home page with login links -->
              ------ login.ejs    <!-- show our login form -->
              ------ signup.ejs   <!-- show our signup form -->
              ------ profile.ejs  <!-- after a user logs in, they will see their profile -->
              - package.json      <!-- handle our npm packages -->
              - server.js         <!-- setup our application -->
      

      Go ahead and create all those files and folders and we’ll fill them in as we go along.

      We are going to install all the packages needed for the entire tutorial series. This means we’ll install all the packages needed for passport local, facebook, twitter, google, and the other things we need. It’s all commented out so you know what each does.

      package.json

          {
            "name": "node-authentication",
            "main": "server.js",
            "dependencies" : {
              "express" : "~4.14.0",
              "ejs" : "~2.5.2",
              "mongoose" : "~4.13.1",
              "passport" : "~0.3.2",
              "passport-local" : "~1.0.0",
              "passport-facebook" : "~2.1.1",
              "passport-twitter" : "~1.0.4",
              "passport-google-oauth" : "~1.0.0",
              "connect-flash" : "~0.1.1",
              "bcrypt-nodejs" : "latest",
      
              "morgan": "~1.7.0",
              "body-parser": "~1.15.2",
              "cookie-parser": "~1.4.3",
              "method-override": "~2.3.6",
              "express-session": "~1.14.1"
            }
          }
      

      Most of these are pretty self-explanatory.

      • Express is the framework.
      • Ejs is the templating engine.
      • Mongoose is object modeling for our MongoDB database.
      • Passport stuff will help us authenticating with different methods.
      • Connect-flash allows for passing session flashdata messages.
      • Bcrypt-nodejs gives us the ability to hash the password.

      I use bcrypt-nodejs instead of bcrypt since it is easier to set up in Windows.

      Now that we have all of our dependencies ready to go, let’s go ahead and install them:

      npm install With all of our packages ready to go, let’s set up our application in server.js.

      Let’s make all our packages work together nicely. Our goal is to set up this file and try to have it bootstrap our entire application. We’d like to not go back into this file if it can be helped. This file will be the glue for our entire application.

      server.js

          
          
          var express  = require('express');
          var app      = express();
          var port     = process.env.PORT || 8080;
          var mongoose = require('mongoose');
          var passport = require('passport');
          var flash    = require('connect-flash');
      
          var morgan       = require('morgan');
          var cookieParser = require('cookie-parser');
          var bodyParser   = require('body-parser');
          var session      = require('express-session');
      
          var configDB = require('./config/database.js');
      
          
          mongoose.connect(configDB.url); 
      
          
      
          
          app.use(morgan('dev')); 
          app.use(cookieParser()); 
          app.use(bodyParser()); 
      
          app.set('view engine', 'ejs'); 
      
          
          app.use(session({ secret: 'ilovescotchscotchyscotchscotch' })); 
          app.use(passport.initialize());
          app.use(passport.session()); 
          app.use(flash()); 
      
          
          require('./app/routes.js')(app, passport); 
      
          
          app.listen(port);
          console.log('The magic happens on port ' + port);
      

      We are going to comment out our passport configuration for now. We’ll uncomment it after we create that config/passport.js file.

      The path of our passport object is important to note here. We will create it at the very beginning of the file with var passport = require('passport');. Then we pass it into our config/passport.js file for it to be configured. Then we pass it to the app/routes.js file for it to be used in our routes.

      Now with this file, we have our application listening on port 8080. All we have to do to start up our server is:

      node server.js Then when we visit http://localhost:8080 we will see our application. (Not really right this moment since we have some more set up to do)

      Auto Refreshing: By default, Node.js doesn’t automatically refresh our server every time we change files. To do that we’ll use nodemon. Just install with: npm install -g nodemon and use with: nodemon server.js.

      Now, this won’t do much for our application since we don’t have our database configuration, routes, user model, or passport configuration set up. Let’s do the database and routes now.

      We already are calling this file in server.js. Now we just have to set it up.

      config/database.js

          module.exports = {
      
              'url' : 'your-settings-here' 
      
          };
      

      Fill this in with your own database. If you don’t have a MongoDB database lying around, I would suggest going to Modulus.io and grabbing one. Once you sign up (and you get a $15 credit for signing up), you can create your database, grab its connection URL, and place it in this file.

      You can also install MongoDB locally and use a local database. You can find instructions here: An Introduction to MongoDB.

      We will keep our routes simple for now. We will have the following routes:

      • Home Page (/)
      • Login Page (/login)
      • Signup Page (/signup)
      • Handle the POST for both login
      • Handle the POST for both signup
      • Profile Page (after logged in)

      app/routes.js

          module.exports = function(app, passport) {
      
              
              
              
              app.get('/', function(req, res) {
                  res.render('index.ejs'); 
              });
      
              
              
              
              
              app.get('/login', function(req, res) {
      
                  
                  res.render('login.ejs', { message: req.flash('loginMessage') });
              });
      
              
              
      
              
              
              
              
              app.get('/signup', function(req, res) {
      
                  
                  res.render('signup.ejs', { message: req.flash('signupMessage') });
              });
      
              
              
      
              
              
              
              
              
              app.get('/profile', isLoggedIn, function(req, res) {
                  res.render('profile.ejs', {
                      user : req.user 
                  });
              });
      
              
              
              
              app.get('/logout', function(req, res) {
                  req.logout();
                  res.redirect('/');
              });
          };
      
          
          function isLoggedIn(req, res, next) {
      
              
              if (req.isAuthenticated())
                  return next();
      
              
              res.redirect('/');
          }
      

      app.post: For now, we will comment out the routes for handling the form POST. We do this since passport isn’t set up yet.

      req.flash: This is the connect-flash way of getting flashdata in the session. We will create the loginMessage inside our passport configuration.

      isLoggedIn: Using route middleware, we can protect the profile section route. A user has to be logged in to access that route. Using the isLoggedIn function, we will kick a user back to the home page if they try to access http://localhost:8080/profile and they are not logged in.

      Logout: We will handle log out by using req.logout() provided by passport. After logging out, redirect the user to the home page.

      With our server running, we can visit our application in our browser athttp://localhost:8080. Once again, we won’t see much since we haven’t made our views. Let’s go do that now. (We’re almost to the authentication stuff, I promise).

      Here we’ll define our views for our home page, login page, and signup/registration page.

      Home Page views/index.ejs

      Our home page will just show links to all our forms of authentication.

      views/index.ejs

          <!doctype html>
          <html>
          <head>
              <title>Node Authentication</title>
              <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> 
              <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> 
              <style>
                  body        { padding-top:80px; }
              </style>
          </head>
          <body>
          <div class="container">
      
              <div class="jumbotron text-center">
                  <h1><span class="fa fa-lock"></span> Node Authentication</h1>
      
                  <p>Login or Register with:</p>
      
                  <a href="/login" class="btn btn-default"><span class="fa fa-user"></span> Local Login</a>
                  <a href="/signup" class="btn btn-default"><span class="fa fa-user"></span> Local Signup</a>
              </div>
      
          </div>
          </body>
          </html>
      

      Now if we visit our app in our browser, we’ll have a site that looks like this:

      Here are the views for our login and signup pages also.

      Login Form views/login.ejs

      views/login.ejs

          <!doctype html>
          <html>
          <head>
              <title>Node Authentication</title>
              <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> 
              <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> 
              <style>
                  body        { padding-top:80px; }
              </style>
          </head>
          <body>
          <div class="container">
      
          <div class="col-sm-6 col-sm-offset-3">
      
              <h1><span class="fa fa-sign-in"></span> Login</h1>
      
              
              <% if (message.length > 0) { %>
                  <div class="alert alert-danger"><%= message %></div>
              <% } %>
      
              
              <form action="/login" method="post">
                  <div class="form-group">
                      <label>Email</label>
                      <input type="text" class="form-control" name="email">
                  </div>
                  <div class="form-group">
                      <label>Password</label>
                      <input type="password" class="form-control" name="password">
                  </div>
      
                  <button type="submit" class="btn btn-warning btn-lg">Login</button>
              </form>
      
              <hr>
      
              <p>Need an account? <a href="/signup">Signup</a></p>
              <p>Or go <a href="/">home</a>.</p>
      
          </div>
      
          </div>
          </body>
          </html>
      

      Signup Form views/signup.ejs

      views/signup.ejs

          <!doctype html>
          <html>
          <head>
              <title>Node Authentication</title>
              <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> 
              <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> 
              <style>
                  body        { padding-top:80px; }
              </style>
          </head>
          <body>
          <div class="container">
      
          <div class="col-sm-6 col-sm-offset-3">
      
              <h1><span class="fa fa-sign-in"></span> Signup</h1>
      
              
              <% if (message.length > 0) { %>
                  <div class="alert alert-danger"><%= message %></div>
              <% } %>
      
              
              <form action="/signup" method="post">
                  <div class="form-group">
                      <label>Email</label>
                      <input type="text" class="form-control" name="email">
                  </div>
                  <div class="form-group">
                      <label>Password</label>
                      <input type="password" class="form-control" name="password">
                  </div>
      
                  <button type="submit" class="btn btn-warning btn-lg">Signup</button>
              </form>
      
              <hr>
      
              <p>Already have an account? <a href="/login">Login</a></p>
              <p>Or go <a href="/">home</a>.</p>
      
          </div>
      
          </div>
          </body>
          </html>
      

      Authenticating With Passport Locally

      Finally! We have finally set up our application and have gotten to the authentication part. Don’t worry. The rest of the authentication articles in this tutorial series will use the same base so we won’t have to set up our application again.

      So far we have installed our packages, set up our application, connected to our database, created our routes, and created our views.

      Now we will create our user model, configure passport for local authentication, and use our configured passport to process our login/signup forms.

      We will create our user model for the entire tutorial series. Our user will have the ability to be linked to multiple social accounts and to a local account. For local accounts, we will be keeping email and password. For the social accounts, we will be keeping their id, token, and some user information.

      You can change these fields out to be whatever you want. You can authenticate locally using username and password (passport-local actually uses username by default but we’ll change that to email).

      app/models/user.js

          
          var mongoose = require('mongoose');
          var bcrypt   = require('bcrypt-nodejs');
      
          
          var userSchema = mongoose.Schema({
      
              local            : {
                  email        : String,
                  password     : String,
              },
              facebook         : {
                  id           : String,
                  token        : String,
                  name         : String,
                  email        : String
              },
              twitter          : {
                  id           : String,
                  token        : String,
                  displayName  : String,
                  username     : String
              },
              google           : {
                  id           : String,
                  token        : String,
                  email        : String,
                  name         : String
              }
      
          });
      
          
          
          userSchema.methods.generateHash = function(password) {
              return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
          };
      
          
          userSchema.methods.validPassword = function(password) {
              return bcrypt.compareSync(password, this.local.password);
          };
      
          
          module.exports = mongoose.model('User', userSchema);
      

      Our model is done. We will be hashing our password within our user model before it saves to the database. This means we don’t have to deal with generating the hash ourselves. It is all handled nicely and neatly inside our user model.

      Let’s move on to the important stuff of this article: authenticating locally!

      Configuring Passport for Local Accounts

      All the configuration for passport will be handled in config/passport.js. We want to keep this code in its own file away from our other main files like routes or the server file. I have seen some implementations where passport will be configured in random places. I believe having it in this config file will keep your overall application clean and concise.

      So far, we have created our passport object in server.js, and then we pass it to our config/passport.js file. This is where we configure our Strategy for local, facebook, twitter, and google. This is also the file where we will create the serializeUser and deserializeUser functions to store our user in session.

      I would highly recommend going to read the passport docs to understand more about how the package works.

      We will be handling login and signup in config/passport.js. Let’s look at signup first.

      config/passport.js

          
          var LocalStrategy   = require('passport-local').Strategy;
      
          
          var User            = require('../app/models/user');
      
          
          module.exports = function(passport) {
      
              
              
              
              
              
      
              
              passport.serializeUser(function(user, done) {
                  done(null, user.id);
              });
      
              
              passport.deserializeUser(function(id, done) {
                  User.findById(id, function(err, user) {
                      done(err, user);
                  });
              });
      
              
              
              
              
              
      
              passport.use('local-signup', new LocalStrategy({
                  
                  usernameField : 'email',
                  passwordField : 'password',
                  passReqToCallback : true 
              },
              function(req, email, password, done) {
      
                  
                  
                  process.nextTick(function() {
      
                  
                  
                  User.findOne({ 'local.email' :  email }, function(err, user) {
                      
                      if (err)
                          return done(err);
      
                      
                      if (user) {
                          return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
                      } else {
      
                          
                          
                          var newUser            = new User();
      
                          
                          newUser.local.email    = email;
                          newUser.local.password = newUser.generateHash(password);
      
                          
                          newUser.save(function(err) {
                              if (err)
                                  throw err;
                              return done(null, newUser);
                          });
                      }
      
                  });
      
                  });
      
              }));
      
          };
      

      We have now provided a strategy to passport called local-signup. We will use this strategy to process our signup form. Let’s open up our app/routes.js and handle the POST for our signup form.

      app/routes.js

          ...
      
              
              app.post('/signup', passport.authenticate('local-signup', {
                  successRedirect : '/profile', 
                  failureRedirect : '/signup', 
                  failureFlash : true 
              }));
      
          ...
      

      That’s all the code we need for the route. All of the heavy-duty stuff lives inside of config/passport.js. All we have to set here is where our failures and successes get redirected. Super clean.

      There is also much more you can do with this. Instead of specifying a successRedirect, you could use a callback and take more control over how your application works. Here is a great stackoverflow answer on error handling. It explains how to use done() and how to be more specific with your handling of a route.

      Testing Signup

      With our passport config finally laid out, we can uncomment that line in our server.js. This will load our config and then we can use our signup form.

      server.js

          ...
      
              
              require('./config/passport')(passport); 
      
          ...
      

      Now that we have passport, our routes, and our redirects in place, let’s go ahead and test our signup form. In your browser, go to http://localhost:8080/signup and fill out your form.

      If all goes according to plan, you should be logged in, your user saved in the session, and you are redirected to the /profile page (the profile page will show nothing right now since we haven’t defined that view).

      If we look in our database, we’ll also see our user sitting there cozily with all the credentials we created for him.

      Exploring Your Database: I use Robomongo to see what’s in my database. Just download it and connect to your database to see your new users after they signup!

      With users able to sign up, let’s give them a way to log in.

      This will be very similar to the signup strategy. We’ll add the strategy to our config/passport.js and the route in app/routes.js.

      config/passport.js

          ...
      
              
              
              
              
              
      
              passport.use('local-login', new LocalStrategy({
                  
                  usernameField : 'email',
                  passwordField : 'password',
                  passReqToCallback : true 
              },
              function(req, email, password, done) { 
      
                  
                  
                  User.findOne({ 'local.email' :  email }, function(err, user) {
                      
                      if (err)
                          return done(err);
      
                      
                      if (!user)
                          return done(null, false, req.flash('loginMessage', 'No user found.')); 
      
                      
                      if (!user.validPassword(password))
                          return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); 
      
                      
                      return done(null, user);
                  });
      
              }));
      
          };
      

      We have now provided a strategy to passport called local-login. We will use this strategy to process our login form. We can check if a user exists, if the password is wrong, and set flash data to show error messages. Let’s open up our app/routes.js and handle the POST for our login form.

      app/routes.js

          ...
      
              // process the login form
              app.post('/login', passport.authenticate('local-login', {
                  successRedirect : '/profile', // redirect to the secure profile section
                  failureRedirect : '/login', // redirect back to the signup page if there is an error
                  failureFlash : true // allow flash messages
              }));
      
          ...
      

      If you try to log in with a user email that doesn’t exist in our database, you will see the error. The same goes for if your password is wrong.

      Now we have functional signup and login forms. If a user is successful in authenticating they will be redirected to the profile page. If they are not successful, they will go home. The last thing we need to do is make our profile page so that those that are lucky enough to signup (all of us?) will have an exclusive place of our site all to themselves.

      views/profile.ejs

          <!doctype html>
          <html>
          <head>
              <title>Node Authentication</title>
              <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
              <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css">
              <style>
                  body        { padding-top:80px; word-wrap:break-word; }
              </style>
          </head>
          <body>
          <div class="container">
      
              <div class="page-header text-center">
                  <h1><span class="fa fa-anchor"></span> Profile Page</h1>
                  <a href="/logout" class="btn btn-default btn-sm">Logout</a>
              </div>
      
              <div class="row">
      
                  
                  <div class="col-sm-6">
                      <div class="well">
                          <h3><span class="fa fa-user"></span> Local</h3>
      
                              <p>
                                  <strong>id</strong>: <%= user._id %><br>
                                  <strong>email</strong>: <%= user.local.email %><br>
                                  <strong>password</strong>: <%= user.local.password %>
                              </p>
      
                      </div>
                  </div>
      
              </div>
      
          </div>
          </body>
          </html>
      

      After a user logs in, they can see all their information. It is grabbed from the session and passed to our view in the app/routes.js file. We will also provide a link to log out.

      There you have it! We’ve built a brand new application from scratch and have the ability to let users signup/register and log in. We even have support for flash messages, hashing passwords, and requiring a login for some sections of our site using route middleware.

      Coming up next we’ll be looking at how to take this same structure, and use passport to authenticate with Facebook, Twitter, and Google. After that, we’ll look at how we can get all these things working together in the same application. Users will be able to log in with one type of account, and then link their other accounts.

      As always, if you see any ways to improve this or need any clarification, sound off in the comments and we’ll respond pretty close to immediately… pretty close.

      How To Install Python 3 and Set Up a Local Programming Environment on Ubuntu 20.04


      Introduction

      Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. First published in 1991 with a name inspired by the British comedy group Monty Python, the development team wanted to make Python a language that was fun to use. Quick to set up, and written in a relatively straightforward style with immediate feedback on errors, Python is a great choice for beginners and experienced developers alike. Python 3 is the most current version of the language and is considered to be the future of Python.

      This tutorial will guide you through installing Python 3 on your local Linux machine and setting up a programming environment via the command line. This tutorial will explicitly cover the installation procedures for Ubuntu 20.04, but the general principles apply to any other distribution of Debian Linux.

      Prerequisites

      You will need a computer or virtual machine with Ubuntu 20.04 installed, as well as have administrative access to that machine and an internet connection. You can download this operating system via the Ubuntu 20.04 releases page.

      Step 1 — Setting Up Python 3

      We’ll be completing our installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.

      The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you do on a computer every day, and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things. The article “An Introduction to the Linux Terminal” can get you better oriented with the terminal.

      On Ubuntu 20.04, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left hand corner of your screen and typing “terminal” into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.

      Ubuntu Terminal

      Ubuntu 20.04 ships with both Python 3 and Python 2 pre-installed. To make sure that our versions are up-to-date, let’s update and upgrade the system with the apt command to work with Ubuntu’s Advanced Packaging Tool:

      • sudo apt update
      • sudo apt -y upgrade

      The -y flag will confirm that we are agreeing that all items to be installed, but depending on your version of Linux, you may need to confirm additional prompts as your system updates and upgrades.

      Once the process is complete, we can check the version of Python 3 that is installed in the system by typing:

      You will receive output in the terminal window that will let you know the version number. The version number may vary, but it will be similar to this:

      Output

      Python 3.8.10

      To manage software packages for Python, let’s install pip, a tool that will install and manage programming packages we may want to use in our development projects. You can learn more about modules or packages that you can install with pip by reading “How To Import Modules in Python 3.”

      • sudo apt install -y python3-pip

      Python packages can be installed by typing:

      • pip3 install package_name

      Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3 install numpy.

      There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

      • sudo apt install build-essential libssl-dev libffi-dev python-dev

      Press y if prompted to do so.

      Once Python is set up, and pip and other tools are installed, we can set up a virtual environment for our development projects.

      Step 2 — Setting Up a Virtual Environment

      Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.

      Setting up a programming environment provides us with greater control over our Python projects and over how different versions of packages are handled. This is especially important when working with third-party packages.

      You can set up as many Python programming environments as you want. Each environment is basically a directory or folder in your computer that has a few scripts in it to make it act as an environment.

      While there are a few ways to achieve a programming environment in Python, we’ll be using the venv module here, which is part of the standard Python 3 library. Let’s install venv by typing:

      • sudo apt install -y python3-venv

      With this installed, we are ready to create environments. Let’s either choose which directory we would like to put our Python programming environments in, or create a new directory with mkdir, as in:

      • mkdir environments
      • cd environments

      Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:

      Essentially, this sets up a new directory that contains a few items which we can view with the ls command:

      Output

      bin include lib lib64 pyvenv.cfg share

      Together, these files work to make sure that your projects are isolated from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs. Python Wheels, a built-package format for Python that can speed up your software production by reducing the number of times you need to compile, will be in the Ubuntu 18.04 share directory.

      To use this environment, you need to activate it, which you can do by typing the following command that calls the activate script:

      • source my_env/bin/activate

      Your prompt will now be prefixed with the name of your environment, in this case it is called my_env. Your prefix may appear somewhat differently, but the name of your environment in parentheses should be the first thing you see on your line:

      This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environment’s settings and packages.

      Note: Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3 if you would prefer. If you use Python 3 on your machine outside of an environment, you will need to use the python3 and pip3 commands exclusively.

      After following these steps, your virtual environment is ready to use.

      Step 3 — Creating a “Hello, World” Program

      Now that we have our virtual environment set up, let’s create a traditional “Hello, World!” program. This will let us test our environment and provides us with the opportunity to become more familiar with Python if we aren’t already.

      To do this, we’ll open up a command-line text editor such as nano and create a new file:

      When the text file opens up in the terminal window we’ll type out our program:

      print("Hello, World!")
      

      Exit nano by typing the CTRL and X keys, and when prompted to save the file press y.

      Once you exit out of nano and return to your shell, we’ll run the program:

      The hello.py program that you created should cause your terminal to produce the following output:

      Output

      Hello, World!

      To leave the environment, type the command deactivate and you will return to your original directory.

      Conclusion

      Congratulations! At this point you have a Python 3 programming environment set up on your local Ubuntu machine and can begin a coding project!

      If you are using a different local machine, refer to the tutorial that is relevant to your operating system in our “How To Install and Set Up a Local Programming Environment for Python 3” series. Alternatively, if you’re using an Ubuntu server, you can follow the “How To Install Python and Set Up a Programming Environment on an Ubuntu 20.04 Server” tutorial.

      With your local machine ready for software development, you can continue to learn more about coding in Python by reading our free How To Code in Python 3 eBook, or consulting our Programming Project tutorials.



      Source link

      How To Install PHP 7.4 and Set Up a Local Development Environment on Ubuntu 18.04


      The author selected Open Sourcing Mental Illness Ltd to receive a donation as part of the Write for DOnations program.

      Introduction

      PHP is a popular server scripting language known for creating dynamic and interactive web pages. Getting up and running with your language of choice is the first step in learning to program.

      This tutorial will guide you through installing PHP 7.4 on Ubuntu and setting up a local programming environment via the command line. You will also install a dependency manager, Composer, and test your installation by running a script.

      Prerequisites

      To complete this tutorial, you will need a local or virtual machine with Ubuntu 18.04 installed and have administrative access and an internet connection to that machine. You can download this operating system via the Ubuntu releases page.

      Step 1 — Setting Up PHP 7.4

      You’ll be completing your installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.

      The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you do on a computer every day and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things. The article An Introduction to the Linux Terminal can get you better oriented with the terminal.

      On Ubuntu, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left-hand corner of your screen and typing terminal into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.

      Ubuntu terminal

      You will want to avoid relying on the default version of PHP because that default version could change depending on where you are running your code. You may also wish to install a different version to match an application you are using or to upgrade to a newer version, such as PHP 8.

      Run the following command to update apt-get itself, which ensures that you have access to the latest versions of anything you want to install:

      Next, install software-properties-common, which adds management for additional software sources:

      • sudo apt -y install software-properties-common

      The -y flag will automatically agree to the installation. Without that, you would receive a prompt in your terminal window for each installation.

      Next, install the repository ppa:ondrej/php, which will give you all your versions of PHP:

      • sudo add-apt-repository ppa:ondrej/php

      Finally, you update apt-get again so your package manager can see the newly listed packages:

      Now you’re ready to install PHP 7.4 using the following command:

      • sudo apt -y install php7.4

      Check the version installed:

      You will receive something similar to the following:

      Output

      PHP 7.4.0beta4 (cli) (built: Aug 28 2019 11:41:49) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies with Zend OPcache v7.4.0beta4, Copyright (c), by Zend Technologies

      Besides PHP itself, you will likely want to install some additional PHP modules. You can use this command to install additional modules, replacing PACKAGE_NAME with the package you wish to install:

      • sudo apt-get install php7.4-PACKAGE_NAME

      You can also install more than one package at a time. Here are a few suggestions of the most common modules you will most likely want to install:

      • sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath

      This command will install the following modules:

      • php7.4-cli - command interpreter, useful for testing PHP scripts from a shell or performing general shell scripting tasks
      • php7.4-json - for working with JSON data
      • php7.4-common - documentation, examples, and common modules for PHP
      • php7.4-mysql - for working with MySQL databases
      • php7.4-zip - for working with compressed files
      • php7.4-gd - for working with images
      • php7.4-mbstring - used to manage non-ASCII strings
      • php7.4-curl - lets you make HTTP requests in PHP
      • php7.4-xml - for working with XML data
      • php7.4-bcmath - used when working with precision floats

      PHP configurations related to Apache are stored in /etc/php/7.4/apache2/php.ini. You can list all loaded PHP modules with the following command:

      You have installed PHP and verified the version you have running. You also installed any required PHP modules and were able to list the modules that you have loaded.

      You could start using PHP right now, but you will likely want to use various libraries to build PHP applications quickly. Before you test your PHP environment, first set up a dependency manager for your projects.

      Step 2 — Setting Up Composer for Dependency Management (Optional)

      Libraries are a collection of code that can help you solve common problems without needing to write everything yourself. Since there are many libraries available, using a dependency manager will help you manage multiple libraries as you become more experienced in writing PHP.

      Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and will manage installing and updating these packages.

      Although similar, Composer is not a package manager in the same sense as yum or apt. It deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default, it does not install anything globally. Thus, it is a dependency manager. It does, however, support a global project for convenience via the global command.

      This idea is not new, and Composer is strongly inspired by Node’s npm and Ruby’s bundler.

      Suppose:

      • You have a project that depends on several libraries.
      • Some of those libraries depend on other libraries.

      Composer:

      • Enables you to declare the libraries you depend on.
      • Finds out which versions of which packages can and need to be installed and installs them by downloading them into your project.
      • Enables you to update all your dependencies in one command.
      • Enables you to see the Basic Usage chapter for more details on declaring dependencies.

      There are, in short, two ways to install Composer: locally as part of your project or globally as a system-wide executable. Either way, you will start with the local install.

      Locally

      To quickly install Composer in the current directory, run this script in your terminal:

      • php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
      • php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
      • php composer-setup.php
      • php -r "unlink('composer-setup.php');"

      This installer script will check some php.ini settings, warn you if they are set incorrectly, and then download the latest composer.phar in the current directory. The four lines will, in order:

      • Download the installer to the current directory
      • Verify the installer SHA-384, which you can also cross-check here
      • Run the installer
      • Remove the installer

      The installer will check a few PHP settings and then download composer.phar to your working directory. This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP that can be run on the command line, amongst other things.

      In order to run Composer, you use php composer.phar. As an example, run this command to see the version of Composer you have installed:

      • php composer.phar --version

      To use Composer locally, you will want your composer.phar file to be in your project’s root directory. You can start in your project directory before installing Composer. You can also move the file after installation. You can also install Composer to a specific directory by using the --install-dir option and additionally (re)name it using the --filename option.

      Since Composer is something used across projects, it’s recommended that you continue to the next portion and set Composer to run globally.

      Globally

      You can place the Composer PHAR anywhere you wish. If you put it in a directory that is part of your $PATH, you can access it globally. You can even make it executable on Ubuntu (and other Unix systems) and invoke it without directly using the PHP interpreter.

      After installing locally, run this command to move composer.phar to a directory that is in your path:

      • sudo mv composer.phar /usr/local/bin/composer

      If you’d like to install it only for your user and avoid requiring root permissions, you can use ~/.local/bin instead, which is available by default on some Linux distributions:

      • mv composer.phar ~/.local/bin/composer

      Now to run Composer, use composer instead of php composer.phar. To check for your Composer version, run:

      As a final step, you may optionally initialize your project with composer init. This will create the composer.json file that will manage your project dependencies. Initializing the project will also let you define project details such as Author and License, and use Composer’s autoload functionality. You can define dependencies now or add them later.

      Run this command to initialize a project:

      Running this command will start the setup wizard. Details that you enter in the wizard can be updated later, so feel free to leave the defaults and just press ENTER. If you aren’t ready to install any dependencies, you can choose no. Enter in your details at each prompt:

      Output

      This command will guide you through creating your composer.json config. Package name (sammy/php_install): sammy/project1 Description []: Author [Sammy <[email protected]>, n to skip]: Minimum Stability []: Package Type (e.g. library, project, metapackage, composer-plugin) []: project License []: Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? no Would you like to define your dev dependencies (require-dev) interactively [yes]? no { "name": "sammy/project1", "type": "project", "authors": [ { "name": "Sammy", "email": "[email protected]" } ], "require": {} } Do you confirm generation [yes]? yes

      Before you confirm the generation, you will see a sample of the composer.json file that the wizard will create. If it all looks good, you can confirm the default of yes. If you need to start over, choose no.

      The first time you define any dependency, Composer will create a vendor folder. All dependencies install into this vendor folder. Composer also creates a composer.lock file. This file specifies the exact version of each dependency and subdependency used in your project. This assures that any machine on which your program is run, will be using the exact same version of each packages.

      Note: The vendor folder should never be committed to your version control system (VCS). The vendor folder only contains packages you have installed from other vendors. Those individual vendors will maintain their own code in their own version control systems. You should only be tracking the code you write. Instead of committing the vendor folder, you only need to commit your composer.json and composer.lock files. You can learn more about ignoring specific files in How To Use Git: A Reference Guide.

      Now that you have PHP installed and a way to manage your project dependencies using Composer, you’re ready to test your environment.

      Step 3 — Testing the PHP Environment

      To test that your system is configured correctly for PHP, you can create and run a basic PHP script. Call this script hello.php:

      This will open a blank file. Put the following text, which is valid PHP code, inside the file:

      hello.php

      <?php
      echo 'Hello World!';
      ?>
      

      Once you’ve added the text, save and close the file. You can do this by holding down the CTRL key and pressing the x key. Then choose y and press ENTER.

      Now you can test to make sure that PHP processes your script correctly. Type php to tell PHP to process the file, followed by the name of the file:

      If the PHP is processed properly, you will see only the characters within the quotes:

      Output

      Hello World!

      PHP has successfully processed the script, meaning that your PHP environment is successfully installed and you’re ready to continue your programming journey.

      Conclusion

      At this point, you have a PHP 7.4 programming environment set up on your local Ubuntu machine and can begin a coding project.

      Before you start coding, you may want to set up an Integrated Development Environment (IDE). While there are many IDEs to choose from, VS Code is a popular choice as it offers many powerful features such as a graphical interface, syntax highlighting, and debugging.

      With your local machine ready for software development, you can continue to learn more about coding in PHP by following How To Work With Strings in PHP.



      Source link